function Stage() {
}
	Stage.prototype.map = null;
	Stage.prototype.name = "me";
	Stage.prototype.uid = 0;
	Stage.prototype.level = 10;
	Stage.prototype.prefix = "m";
	Stage.prototype.myfriends = new Array();
	Stage.prototype.timer = window;
	Stage.prototype.isLocalStorage = localStorage != null;
	Stage.prototype.myMarker = null;
	Stage.prototype.currentLocation = null;
	Stage.prototype.markers = new Array();
	Stage.prototype.infowindow = new google.maps.InfoWindow();
	Stage.prototype.sylvekClient = new SylvekClient();

Stage.prototype.addPoint = function(marker, name, uid, update) {
	var myHtml = "<strong>";
	myHtml += "<a href=http://" + prefix + ".facebook.com/profile.php?id=";
	myHtml += uid;
	myHtml += " target=_parent>Watch the profile of " + name;
	myHtml += "</a></strong>";
	myHtml += "<p><a href=http://" + prefix + ".facebook.com/addfriend.php?id=";
	myHtml += uid;
	myHtml += " target=_parent>Add as friend</a></p>";
	myHtml += "<p><a href=http://" + prefix + ".facebook.com/inbox/?compose&id=";
	myHtml += uid;
	myHtml += " target=_parent>Send a message</a></p>";
	myHtml += "<p>" + update + "</p>";
	
	google.maps.event.addListener(marker, "click", function() {
		this.infowindow.setContent(myHtml);
		this.infowindow.open(map,marker);
	});
}

Stage.prototype.clearAll = function() {
	while(this.markers.length > 0) {
		var marker = this.markers.pop();
		google.maps.event.clearInstanceListeners(marker);
		marker.setMap(null); // remove
	}
}

Stage.prototype.updateMyPosition = function(position) {
  var coords = position.coords;
  var p = new google.maps.LatLng(coords.latitude, coords.longitude);
  this.map.setCenter(p);
  this.map.setZoom(this.level);
  var now = new Date();
  this.myMarker.setPosition(p);
  this.addPoint(this.myMarker, "you", this.uid, now.toLocaleString());
  this.sylvekClient.update(
  	coords.latitude, 
  	coords.longitude, 
  	this.name, this.uid, false,
  	this.handleInfo, this.handleInfo);
  	
  this.currentLocation = coords;
}

Stage.prototype.handleError = function(positionError) {
  alert('Attempt to get location failed: ' + positionError.message);
}

Stage.prototype.handleInfo = function(message) {
	document.getElementById("info").style.display = "block";
	document.getElementById("info").innerHTML = message;
	this.timer.setTimeout(function() { document.getElementById("info").style.display = "none"; }, 3000);
}

Stage.prototype.initialize = function() {
 	var latlng = new google.maps.LatLng(0, 0);
    var myOptions = {
       zoom: 3,
       center: latlng,
       mapTypeId: google.maps.MapTypeId.ROADMAP
    };
    this.map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
    this.myMarker = new google.maps.Marker({position: latlng, map: this.map}); 
 }

Stage.prototype.populate = function(url) {
	this.sylvekClient.getPeople(
		url,
		function(person) {
			if(person.uid != uid) {
				var point = new google.maps.LatLng(person.latitude,person.longitude);
				var online = person.online == 1;

				var marker = new google.maps.Marker({position: point, map: this.map});
				if(online) {marker.setIcon('http://labs.google.com/ridefinder/images/mm_20_red.png');} 
				else {marker.setIcon('http://labs.google.com/ridefinder/images/mm_20_blue.png');}
				
			    this.addPoint(marker, person.name, person.uid, person.update);
				this.markers.push(marker);
				this.handleInfo(person.name + " found =)");
			}
		}, 
		this.handleInfo);
    
}

Stage.prototype.showTime = function() {
	if(geo_position_js.init()) {
		geo_position_js.getCurrentPosition(
			this.updateMyPosition, 
			this.handleError, 
			{maximumAge:0});
	} else {
		this.handleError("something is wrong, unable to init geolocation feature");
	}
}

Stage.prototype.unregister = function() {
	this.remove(this.uid, this.handleInfo, this.handleInfo);
	window.location.href="/index.html";
}

Stage.prototype.share = function(subject, body) {
	if(this.currentLocation == null) {
		this.handleError("current location is unknown, please try later.");
		return;
	}
	
	window.location.href=
		"mailto:?subject=Où? allows you to find your friends" +
		"&body=http://ou-android.appspot.com/static.jsp?pos=" +
		this.currentLocation.latitude + "," + this.currentLocation.longitude;
}

Stage.prototype.selectFriends = function(type) {
	
	this.clearAll();
	
	switch (type) {
	default:
	case "nearme":
		if(this.currentLocation == null) {
			this.handleError("current location is unknown, please try later.");
		} else {
			this.populate('http://ou-android.appspot.com/rest/uids?kind=nearme&latitude=' 
			+ this.currentLocation.latitude 
			+ '&longitude=' 
			+ this.currentLocation.longitude);
		}
		break;
	case "all":
		this.populate('http://ou-android.appspot.com/rest/uids?kind=all');
		break;
	case "online":
		this.populate('http://ou-android.appspot.com/rest/uids?kind=online');
		break;
	case "myfriends":
		for(var i=0;i<this.myfriends.length;i++) {
			this.sylvekClient.getPerson(
				this.myfriends[i], 
				function(person) {
					if(person != null) {
						var point = new google.maps.LatLng(person.latitude,person.longitude);
						var online = person.online == 1;

						var marker = new google.maps.Marker({position: point, map: this.map});
						if(online) {marker.setIcon('images/red.png');} 
						else {marker.setIcon('images/blue.png');}
						
					    this.addPoint(marker, person.name, person.uid, person.update);
						this.markers.push(marker);
						this.handleInfo(person.name + " found =)");
					}
				},
				this.handleInfo);
		}
		break;
	}
}
